home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 June: Reference Library / Dev.CD Jun 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 24 / develop issue 24 code / scriptable database 1.0a15.sea / Scriptable Database 1.0a15 / Database / Transaction.h / Transaction.h
Encoding:
Text File  |  1996-04-29  |  4.3 KB  |  141 lines  |  [TEXT/CWIE]

  1. //================================================================================
  2. // Greg Anderson
  3. // db+
  4. //
  5. // Transaction object
  6. // 16, 17 May 1994
  7. //================================================================================
  8. #pragma once
  9.  
  10. #ifndef __TRANSACTION__
  11. #define __TRANSACTION__
  12.  
  13. #include "ReferenceTemplates.h"
  14.  
  15. //
  16. // For REQUIREVALIDPOINTER
  17. //
  18. #include "Exceptions.h"
  19.  
  20. //
  21. // For nil
  22. //
  23. #include <Types.h>
  24.  
  25. class TTransactionAwareObject;
  26.  
  27. //
  28. // Sticking with our technique of typesafe downcasting,
  29. // a transaction object must know about all of the
  30. // different object types and update pointer types that
  31. // it will ever need to deal with.
  32. //
  33. class TDBRecord;
  34. class TDBElement;
  35. class TDBProperty;
  36. class TDataRecord;
  37.  
  38. //================================================================================
  39. // Class TUpdatePointerCollectionElement
  40. //
  41. // An update pointer collection element stores one item in a collection of
  42. // update pointers.
  43. //================================================================================
  44. class TUpdatePointerCollectionElement
  45. {
  46. private:
  47.     TTransactionAwareObject*            fUpdatePointer;
  48.     TUpdatePointerCollectionElement*    fNext;
  49.  
  50. public:
  51.     TUpdatePointerCollectionElement(TTransactionAwareObject* uptr, TUpdatePointerCollectionElement* chain) : fUpdatePointer(uptr), fNext(chain) { REQUIREVALIDPOINTER(uptr); }
  52.     ~TUpdatePointerCollectionElement() { delete fNext; }
  53.     
  54.     TUpdatePointerCollectionElement* Next() { return fNext; }
  55.     TTransactionAwareObject* UpdatePointer() { return fUpdatePointer; }
  56. };
  57.  
  58. //================================================================================
  59. // Class TUpdatePointerCollection
  60. //
  61. // An update pointer collection keeps track of the update pointers that are
  62. // part of a given transaction.
  63. //================================================================================
  64. class TUpdatePointerCollection
  65. {
  66. private:
  67.     TUpdatePointerCollectionElement*        fFirst;
  68.  
  69. public:
  70.     TUpdatePointerCollection() : fFirst(nil) {}
  71.  
  72.     ~TUpdatePointerCollection()
  73.     {        
  74.         this->ClearUpdatePointerCollection();
  75.     }
  76.  
  77.     void ClearUpdatePointerCollection()
  78.     {        
  79.         delete fFirst;
  80.         fFirst = nil;
  81.     }
  82.     
  83.     TUpdatePointerCollectionElement*        First()    { return fFirst; }
  84.     
  85.     void Add(TTransactionAwareObject* up)
  86.     {
  87.         //
  88.         // We should throw if up is nil
  89.         // (Actually, we should have already thrown before getting here)
  90.         //
  91.         if(up != nil)
  92.         {
  93.             TUpdatePointerCollectionElement* newUPCElement = new TUpdatePointerCollectionElement(up, fFirst);
  94.             fFirst = newUPCElement;
  95.         }
  96.     }
  97. };
  98.  
  99. //================================================================================
  100. // Class TUpdatePointerCollectinIterator
  101. //================================================================================
  102. class TUpdatePointerCollectionIterator
  103. {
  104. private:
  105.     TUpdatePointerCollection*                fUPCollection;
  106.     TUpdatePointerCollectionElement*        fCurrent;
  107.  
  108. public:
  109.     TUpdatePointerCollectionIterator(TUpdatePointerCollection* base) : fUPCollection(base), fCurrent(base->First()) { REQUIREVALIDPOINTER(base); }
  110.  
  111.     TUpdatePointerCollectionElement*        Current()    { REQUIREVALIDPOINTER(fCurrent); return fCurrent; }
  112.     Boolean                                    More()        { return fCurrent != nil; }
  113.     void                                    Next()        { if(fCurrent) fCurrent = fCurrent->Next(); }
  114. };
  115.  
  116. //================================================================================
  117. // Class TTransaction
  118. //================================================================================
  119. class TTransaction : public TUpdatePointerCollection
  120. {
  121. public:
  122.                                 ~TTransaction();
  123.  
  124.     TTransactionAwareObject*    GetUpdatePointer(AConst<TTransactionAwareObject> object);
  125.     AnUpdate<TDBRecord>            GetDBRecordUpdatePointer(AConst<TTransactionAwareObject> cursor);
  126.     AnUpdate<TDBElement>        GetDBElementUpdatePointer(AConst<TTransactionAwareObject> cursor);
  127.     AnUpdate<TDBProperty>        GetDBPropertyUpdatePointer(AConst<TTransactionAwareObject> cursor);
  128.     AnUpdate<TDataRecord>        GetDataRecordUpdatePointer(AConst<TTransactionAwareObject> cursor);
  129.  
  130.  
  131.     AnUpdate<TDBRecord>            GetDBRecordUpdatePointer(AConst<TDBRecord> cursor);
  132.     AnUpdate<TDBElement>        GetDBElementUpdatePointer(AConst<TDBElement> cursor);
  133.     AnUpdate<TDBProperty>        GetDBPropertyUpdatePointer(AConst<TDBProperty> cursor);
  134.     AnUpdate<TDataRecord>        GetDataRecordUpdatePointer(AConst<TDataRecord> cursor);
  135.  
  136.     void                        CommitChanges();
  137.     void                        DiscardChanges();
  138. };
  139.  
  140. #endif
  141.